from PyQt4 import QtGui;

class ChangeEdgeTargetCommand(QtGui.QUndoCommand):
    def __init__(self, gEdge=None, graphicsScene=None, gNewTarget=None):
        msgUndo = "Change Edge Target";
        super(ChangeEdgeTargetCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
        self.gEdge = gEdge;
        self.oldTarget = gEdge.objTo;
        self.newTarget = gNewTarget;
    
    def undo(self):
        self.gEdge.changeTarget(self.oldTarget);
    
    def redo(self):
        self.gEdge.changeTarget(self.newTarget);


class AddEdgeCommand(QtGui.QUndoCommand):
    def __init__(self, gEdges=None, graphicsScene=None, bAddedFromLink=True):
        self.gEdges = gEdges;
        if len(gEdges) > 1:
            msgUndo = "Add Multiple Edges";
        else:
            msgUndo = "Add Edge";
        super(AddEdgeCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
        self.bAddedFromLink = bAddedFromLink;
    
    def undo(self):
        for i in range(len(self.gEdges)):
            self.gEdges[i].removeEdge();
            self.graphicsScene.removeItem(self.gEdges[i]);
    
    def redo(self):
        for i in range(len(self.gEdges)):
            self.gEdges[i].addEdge();
            if not self.bAddedFromLink:
                self.graphicsScene.addItem(self.gEdges[i]);
        self.bAddedFromLink = False;


class DeleteEdgeCommand(QtGui.QUndoCommand):
    def __init__(self, gSelectedEdges=None, graphicsScene=None):
        self.selectedEdges = gSelectedEdges;
        if len(gSelectedEdges) > 1:
            msgUndo = "Delete Multiple Edges";
        else:
            msgUndo = "Delete Edge";
        super(DeleteEdgeCommand, self).__init__(msgUndo);
        self.graphicsScene = graphicsScene;
    
    def undo(self):
        for i in range(len(self.selectedEdges)):
            self.selectedEdges[i].addEdge();
            self.graphicsScene.addItem(self.selectedEdges[i]);
    
    def redo(self):
        for i in range(len(self.selectedEdges)-1,-1,-1):
#            print len(self.selectedEdges), range(len(self.selectedEdges)-1,-1,-1), self.selectedEdges
            self.graphicsScene.removeItem(self.selectedEdges[i]);
            self.selectedEdges[i].removeEdge();
#            print len(self.selectedEdges), range(len(self.selectedEdges)-1,-1,-1), self.selectedEdges
#            if i< len(self.selectedEdges):
